define(function(require, exports, module){
    $(function(){
		app.edit_password = function(){
			console.log('edit_password');
		}
		app.edit_userinfo = function(){
			console.log('edit_userinfo');
		}
		app.logout = function(isTrue){
			app.logout.obj = new $.zui.ModalTrigger({
				title : '确定要退出登录吗？',
				custom: '#myModalAlert',
				size : 'sm',
			})
			app.logout.obj.show();
		}


		var $imain = $('#imain');
    	var iframe_href = '';
		makeMenu(app.menu);

		// nav菜单切换
	    $('#navmenu').on('click', 'a', function(){
	        var $this = $(this);
	        if(!$this.parent().hasClass('active')){
	            var index = $this.data('menu-index');
	            makeSidebar(app.menu[index], index);
	            findIframeActive();
	            $('#navmenu').children('li').removeClass('active')
	                	.eq(index).addClass('active');
	        }
	    });

	    $('#sidebar').on('click', 'a', function(){
        	iframeGo($(this).attr('href'))
        	return false;
        })

	    // 二级菜单收缩
	    var $sidebar = $('#sidebar').on('click', '.sidebar-nav-titel', function(){
	        var $this = $(this).parent().children('.sidebar-nav-group').slideToggle(100, function(){
	            $this.parent().toggleClass('sidebar-hide');
	        });

	    })

	    // 大小侧栏变化
	    .on('click', '.sidebar-fold', function(){
	        sidebar_mini = !$sidebar.hasClass('sidebar-mini');
	        localStorage.sidebar_mini = ~~sidebar_mini;
	        $sidebar.toggleClass('sidebar-mini');
	    })

	    // 小侧栏tooltip
	    .on('mouseover', '.sidebar-nav-item, .sidebar-nav-titel', function(){
	        if(sidebar_mini){
	            var $this = $(this);
	            $this.tooltip('show', $this.children('.sidebar-nav-text').text());
	        }
	    })

	    var sidebar_mini = ~~localStorage.sidebar_mini;
	    if(sidebar_mini){
	        $sidebar.find('.sidebar-fold').trigger('click');
	    }

	    // 刷新按钮
	    $('.navbar-right .btn-refresh').click(function(){
	    	iframeGo(iframe_href, true)
	    })


	    $imain.on('load', findIframeActive)
	    
	    function findIframeActive(){
	    	iframe_href = $imain[0].contentWindow.location.href;
        	$sidebar.find('a').removeClass('active').each(function(i,row){
        		var $this = $(row);
        		if(iframe_href.indexOf($this.attr('href')) >= 0){
        			$this.addClass('active');
        		}

        	});
	    }

	    function iframeGo(url, refresh){
	    	if(iframe_href != url || refresh){
	    		ver = localStorage.console_ver;
	    		if(refresh){
	    			ver = ver ? ~~ver + 1 : 1;
	    		}
	    		localStorage.console_ver = ver;
	    		url += (url.indexOf('?') >= 0 ? '&' : '?') + 'tmpver=' + ver;
	        	$imain[0].src = url;
	    	}
	    }


	    function  makeMenu(menu){
	        var index = 0;
	        var _html = '';
	        menu.forEach(function(item, i){
	            var className = i == index ? 'active' : '';
	            var href = item.child[0].child[0].link;
	            _html += '<li class="'+ className +'"><a href="#" data-href="'+ href +'" data-menu-index="'+ i +'">'+ item.name +'</a></li>';
	        })

	        $('#navmenu').html(_html);
	        makeSidebar(app.menu[index], index);
	    }

	    function makeSidebar(item, index){
	        var _html = '<div class="sidebar-fold text-center"><i class="icon icon-bars"></i></div>';
	        var hasClassName = false;
	        item.child.forEach(function(row, i){
	            _html += '<dl class="sidebar-nav">'
                    + '<dt class="sidebar-nav-titel" data-placement="right">'
                        + '<i class="caret transition"></i>'
                        + '<span class="sidebar-nav-text">'+ row.name +'</span>'
                    + '</dt>'
                    + '<dd class="sidebar-nav-group">';

                row.child.forEach(function(el, ii){
                	var className = el.link == iframe_href ? 'active' : '';
                	// console.log(iframe_href, el.link, className);
                    _html += 
                        '<a class="sidebar-nav-item '+ className +'" href="'+ el.link +'" data-placement="right">'
                            + '<i class="icon '+ el.icon +'"></i> '
                            + '<span class="sidebar-nav-text">'+ el.name +'</span>'
                        + '</a>'
                })

	            _html += '</dd>'
	                + '</dl>'
	        })

	        $('#sidebar').html(_html);
	        if(!iframe_href){
	        	iframeGo(item.child[0].child[0].link)
	        }
	    }

    })
});